package com.linsir.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.linsir.core.service.impl.BaseServiceImpl;
import com.linsir.core.util.V;
import com.linsir.file.dto.UploadFileResult;
import com.linsir.file.entity.UploadFile;
import com.linsir.file.mapper.UploadFileMapper;
import com.linsir.file.service.FileStorageService;
import com.linsir.file.service.UploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author ：linsir
 * @date ：Created in 2022/10/2 21:06
 * @description：上传
 * @modified By：
 * @version: 0.0.1
 */
@Service
public class UploadFileServiceImpl extends BaseServiceImpl<UploadFileMapper,UploadFile> implements UploadFileService {

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public List<UploadFile> getUploadedFiles(String relObjClass, Object relObjId) {
        LambdaQueryWrapper<UploadFile> queryWrapper = new QueryWrapper<UploadFile>().lambda()
                .eq(UploadFile::getRelObjType, relObjClass)
                .eq(UploadFile::getRelObjId, relObjId.toString())
                .orderByAsc(UploadFile::getCreatedTime);
        return getEntityList(queryWrapper);
    }

    @Override
    public void bindRelObjId(Object relObjId, Class<?> relObjTypeClass, List<String> fileUuidList) throws Exception {
        // 如果fileUuidList为null，不进行绑定关联更新
        if (fileUuidList == null) {
            return;
        }
        // 删除 relObjId + relObjType下的 并且不包含fileIdList的file。
        this.update(
                Wrappers.<UploadFile>lambdaUpdate()
                        .set(UploadFile::isDeleted, true)
                        .eq(UploadFile::getRelObjId, relObjId.toString())
                        .eq(UploadFile::getRelObjType, relObjTypeClass.getSimpleName())
                        .notIn(V.notEmpty(fileUuidList), UploadFile::getId, fileUuidList)
        );
        // 绑定绑定数据
        if (V.notEmpty(fileUuidList)) {
            this.update(
                    Wrappers.<UploadFile>lambdaUpdate()
                            .set(UploadFile::getRelObjId, relObjId.toString())
                            .in(UploadFile::getId, fileUuidList)
            );
        }
    }


    @Override
    public UploadFileResult upload(MultipartFile multipartFile) throws Exception {
        UploadFileResult uploadFileResult = null;
        UploadFile uploadFile = new UploadFile();
        try {
            uploadFileResult = fileStorageService.storage(multipartFile);
            uploadFile.setFileName(uploadFileResult.getFileName());
            uploadFile.setAccessUrl(uploadFileResult.getStorageFullPath());
            uploadFile.setFileType(uploadFileResult.getExt());
            createEntity(uploadFile);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return uploadFileResult;
    }

    @Override
    public UploadFileResult imgUpload(MultipartFile multipartFile) {
        return null;
    }


}
